<?php
class Bizprofile_Models_Dao_Pdo_Mysql_Permission extends Tomato_Model_Dao
	implements Bizprofile_Models_Interface_Permission
{
	public $_general_info = array(
					'contact_email' 		=> 1,
					'contact_tel'			=> 1,
					'contact_fax'			=> 1,
					'contact_address'		=> 1,
					'bank_account_vnd'  	=> 1,
					'bank_account_usd'		=> 1,
					'registration_number' 	=> 1,
					'professional'			=> 1
				);
								
	public $_enterprise_info = array(1,1,1,1,1);
	
	public function convert($entity)
	{
		return new Bizprofile_Models_Permission($entity); 
	}
	
	public function getPermission($enterprise_id) 
	{
		$rs = $this->_conn
					->select()
            		->from(array('p' => $this->_prefix . 'enterprise_permission'))
            		->where('p.enterprise_id = ?', $enterprise_id)
					->order('user_entity ASC')
					->query()
					->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function add($permission) 
	{
		$this->_conn->insert($this->_prefix . 'enterprise_permission', 
							array(
								'enterprise_id' 	 		=> $permission->enterprise_id,
								'user_entity' 				=> $permission->user_entity,
								'general_info' 	 	 		=> $permission->general_info,							
								'enterprise_info' 	 	 	=> $permission->enterprise_info,							
								'media_permission' 	 	 	=> $permission->media_permission,							
								'blog_permission' 	 	 	=> $permission->blog_permission,							
								'partner_permission' 	 	=> $permission->partner_permission,							
								'messaging_permission' 	 	=> $permission->messaging_permission							
							));
		return $this->_conn->lastInsertId($this->_prefix . 'enterprise_permission');
	}
	
	public function update($permission) 
	{
		$data = array(			
			'general_info' 	 	 		=> $permission->general_info,							
			'enterprise_info' 	 	 	=> $permission->enterprise_info,							
			'media_permission' 	 	 	=> $permission->media_permission,							
			'blog_permission' 	 	 	=> $permission->blog_permission,							
			'partner_permission' 	 	=> $permission->partner_permission,							
			'messaging_permission' 	 	=> $permission->messaging_permission
		);		
		return $this->_conn->update($this->_prefix . 'enterprise_permission', 
									$data, 
									array(
										'enterprise_id = ?' => $permission->enterprise_id,
										'user_entity = ?'	=> $permission->user_entity
									));
	}
	
	public function getRoles(){
		$rs = $this->_conn
					->select()
            		->from(array('r' => $this->_prefix . 'enterprise_role'))
            		->where('r.name != ?', 'owner')
            		->order('role_id ASC')
					->query()
					->fetchAll();
		return new Tomato_Model_RecordSet($rs, $this);
	}
	
	public function getUserEntity($user_id, $enterprise_id){
		if($user_id == 0){
			return 1; //nologin
		}
		$account = $this->_conn
					->select()
					->from(array('r' => $this->_prefix . 'enterprise_user_accounts'))
					->where('r.user_id = ?', $user_id)
					->limit(1)
					->query()
					->fetch();
		if($account == null){
			return 2; //visitor
		}
		if($account->enterprise_id == $enterprise_id){
			return 0; //owner
		}
		//Check partner
		$partner = $this->_conn
					->select()
					->from(array('r' => $this->_prefix . 'enterprise_list'))
					->where('r.enterprise_id = ?', $account->enterprise_id)
					->where('r.relationid = ?', $enterprise_id)
					->where('r.partner_type = ?', 'buddy')
					->limit(1)
					->query()
					->fetch();
		if($partner != null){
			return 4;		
		}
		if($account->enterprise_id != $enterprise_id){
			return 3; //enterprise visitor
		}
		return -1; //owner
	}
	
	public function getPermissionByUserEntity($enterprise_id, $user_entity){		
		 $select    = $this->_conn
					->select()
					->from(array('r' => $this->_prefix . 'enterprise_permission'));
		if($enterprise_id)
			$select->where('r.enterprise_id = ?', $enterprise_id);
		if($user_entity)	
			$select->where('r.user_entity = ?', $user_entity);
					
		$row = $select->limit(1)
					  ->query()
					  ->fetch();		
		return (null == $row) ? null : new Bizprofile_Models_Permission($row);
	}
}
